<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Api_V2_Card extends Controller_Pc_Api_V2_Base
{

    private $result;
    private $typeid = 198;
    public function before()
    {
        parent::before();
        // 检查会员信息
        $token       = Common::remove_xss($this->request_data->token);
        $check_token = Model_Api_V2_Pub::token_decode($token);
        if ($check_token['status']) {
            $this->mid = $check_token['mid'];
        }
        $this->result = array(
            'status' => true,//默认状态为成功
            'msg' => '操作成功',//默认的提示消息
            'data' => array()
        );
    }

    /**
     * @return void
     * 卡列表
     */
    public function action_card_list()
    {
        try {
            $data = array(
                'page'      => $this->request_data->page,
                'page_size' => $this->request_data->page_size
            );
            $list = Model_Api_V2_Card::card_list($data);

            $this->result['data'] = $list;
        }catch (Exception $e){
            $this->result['status'] = false;
            $this->result['msg'] = $e->getMessage();
        }

        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    /**
     * @return void
     * 卡详情
     */
    public function action_card_detail()
    {
        try {
            $data = array(
                'card_id'        => $this->request_data->card_id,
                'member_card_id' => $this->request_data->member_card_id,
            );
            if(!$data['card_id'] && !$data['member_card_id']){
                throw new Exception('参数错误');
            }
            if($data['card_id']){
                $detail = Model_Api_V2_Card::card_detail($data);
            }else{
                $detail = Model_Api_V2_Card::member_card_detail($data);
            }

            $this->result['data'] = $detail;
        }catch (Exception $e){
            $this->result['status'] = false;
            $this->result['msg'] = $e->getMessage();
        }

        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    /**
     * @return void
     * 下单
     */
    public function action_card_book()
    {
        try {
            if(!$this->mid){
                throw new Exception('请先登录');
            }
            $data = array(
                'card_id'   => $this->request_data->card_id,
                'tourer_id' => $this->request_data->tourer_id,
                'member_id' => $this->mid,
            );
            if(!$data['card_id'] || !$data['tourer_id']){
                throw new Exception('参数错误');
            }
            $res = Model_Api_V2_Card::card_book($data);

            $this->result = $res;
        }catch (Exception $e){
            $this->result['status'] = false;
            $this->result['msg'] = $e->getMessage();
        }

        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    /**
     * @return void
     * 已购卡列表
     */
    public function action_member_card_list()
    {
        try {
            if(!$this->mid){
                throw new Exception('请先登录');
            }
            $data = array(
                'member_id' => $this->mid,
            );
            $list = Model_Api_V2_Card::member_card_list($data);

            $this->result['data'] = $list;
        }catch (Exception $e){
            $this->result['status'] = false;
            $this->result['msg'] = $e->getMessage();
        }

        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    /**
     * @return void
     *
     */
    public function action_card_product_list()
    {
        try {
            if(!$this->mid){
                throw new Exception('请先登录');
            }
            $data = array(
                'card_id'        => $this->request_data->card_id,
                'dest_id'        => $this->request_data->dest_id,
                'keyword'        => $this->request_data->keyword,
                'page'           => $this->request_data->page,
                'page_size'      => $this->request_data->page_size,
            );
            if(!$data['card_id']){
                throw new Exception('参数错误');
            }
            $product = Model_Api_V2_Card::card_product_list($data);

            $this->result['data'] = $product;
        }catch (Exception $e){
            $this->result['status'] = false;
            $this->result['msg'] = $e->getMessage();
        }

        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    /**
     * @return void
     * 已购卡产品列表
     */
    public function action_member_card_product_list()
    {
        try {
            if(!$this->mid){
                throw new Exception('请先登录');
            }
            $data = array(
                'member_card_id' => $this->request_data->member_card_id,
                'dest_id'        => $this->request_data->dest_id,
                'keyword'        => $this->request_data->keyword,
                'page'           => $this->request_data->page,
                'page_size'      => $this->request_data->page_size,
            );
            if(!$data['member_card_id']){
                throw new Exception('参数错误');
            }
            $product = Model_Api_V2_Card::member_card_product_list($data);

            $this->result['data'] = $product;
        }catch (Exception $e){
            $this->result['status'] = false;
            $this->result['msg'] = $e->getMessage();
        }

        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    public function action_card_product_detail_number()
    {
        try {
            if(!$this->mid){
                throw new Exception('请先登录');
            }
            $data = array(
                'member_card_id' => $this->request_data->member_card_id,
                'product_id'     => $this->request_data->product_id,
                'suit_id'        => $this->request_data->suit_id,
                'type_id'        => $this->request_data->type_id,
                'start_date'     => $this->request_data->start_date
            );
            if(!$data['member_card_id']){
                throw new Exception('参数错误');
            }
            $number = Model_Card::member_card_product_detail($data);

            $this->result['data']['number'] = $number;
        }catch (Exception $e){
            $this->result['status'] = false;
            $this->result['msg'] = $e->getMessage();
        }

        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    /**
     * @return void
     * 实名列表
     */
    public function action_card_people_list()
    {
        try {
            if(!$this->mid){
                throw new Exception('请先登录');
            }
            $data = array(
                'member_card_id' => $this->request_data->member_card_id,
            );
            if(!$data['member_card_id']){
                throw new Exception('参数错误');
            }
            $list = Model_Api_V2_Card::card_people_list($data);

            $this->result['data'] = $list;
        }catch (Exception $e){
            $this->result['status'] = false;
            $this->result['msg'] = $e->getMessage();
        }

        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    /**
     * @return void
     * 实名审核申请
     */
    public function action_card_realname_audit()
    {
        try {
            if(!$this->mid){
                throw new Exception('请先登录');
            }
            $data = array(
                'id'               => $this->request_data->id,
                'name'             => $this->request_data->name,
                'id_card_no'       => $this->request_data->id_card_no,
                'id_card_front'    => $this->request_data->id_card_front,
                'id_card_opposite' => $this->request_data->id_card_opposite,
            );
            if(!$data['id'] || !$data['id_card_no'] || !$data['id_card_front'] || !$data['id_card_opposite']){
                throw new Exception('参数错误');
            }
            if(mb_strlen($data['name'])>5){
                throw new Exception('姓名过长');
            }
            if (!preg_match('/^(\d{17}[\dXx]|\d{15})$/', $data['id_card_no'])) {
                throw new Exception('身份证号格式不正确');
            }
            Model_Api_V2_Card::card_realname_audit($data);
        }catch (Exception $e){
            $this->result['status'] = false;
            $this->result['msg'] = $e->getMessage();
        }

        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    public function action_card_reservation_record()
    {
        try {
            if(!$this->mid){
                throw new Exception('请先登录');
            }
            $data = array(
                'member_card_id' => $this->request_data->member_card_id,
                'page'           => $this->request_data->page,
                'page_size'      => $this->request_data->page_size,
                'type'           => $this->request_data->type,
            );
            $list = Model_Api_V2_Card::card_reservation_record($data);

            $this->result['data'] = $list;
        }catch (Exception $e){
            $this->result['status'] = false;
            $this->result['msg'] = $e->getMessage();
        }

        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    /**
     * @return void
     * 取消订单
     */
    public function action_cancel_order()
    {
        try {
            $order_sn = $this->request_data->ordersn;
            if(!$order_sn){
                throw new Exception('订单号缺失');
            }
            $order = DB::select()->from('member_order')->where("ordersn='{$order_sn}'")->execute()->current();
            if($order['status']==5){
                throw new Exception('该订单已核销，不可取消');
            }
            if($order['typeid']==21){
                $sub_order_sn = DB::select()->from('member_order')->where("pid={$order['id']}")->execute()->get('ordersn');
                $card_order = DB::select()->from('card_member_reservation_record')->where("order_sn='{$sub_order_sn}'")->execute()->current();
            }else{
                $card_order = DB::select()->from('card_member_reservation_record')->where("order_sn='{$order_sn}'")->execute()->current();
            }

            if(!$card_order){
                throw new Exception('该订单非会员卡预订');
            }
            if(in_array($order['status'],array(3,4))){
                throw new Exception('该订单已取消');
            }
            $res = DB::update('member_order')->set(array('status'=>3))->where("id={$order['id']}")->execute();
            if($res){
                $member_card = array('id'=>$card_order['member_card_id']);
                $dingnum = intval($order['dingnum']) + intval($order['childnum']) + intval($order['oldnum']) + intval($order['people4_num']) + intval($order['people5_num']) + intval($order['people6_num']);
                call_user_func(array('Model_Card', 'card_product_storage'), '+' . $dingnum, $member_card, $order);
            }
            $this->result['status'] = true;
        }catch (Exception $e){
            $this->result['status'] = false;
            $this->result['msg'] = $e->getMessage();
        }
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }
}